<template>
    <section @dblclick="tables = true">
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.keyWords" placeholder="数据字典名"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="search">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表-->
        <el-table :data="departments"  highlight-current-row  @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60">
            </el-table-column>
            <el-table-column prop="name" label="数据字典名" width="400" @dblclick="tables = true" sortable>
            </el-table-column>
            <el-table-column prop="sn" label="编号" width="400" sortable>
            </el-table-column>
            <el-table-column label="操作">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next"
                           @current-change="handleCurrentChange"
                           :page-size="pageSize" :total="total"
                           :current-page="currentPage"
                           style="float:right;">
            </el-pagination>
        </el-col>

        <!--新增/修改界面-->
        <el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
            <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
                <el-form-item label="字典名" prop="name">
                    <el-input v-model="saveForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="编号" prop="sn">
                    <el-input v-model="saveForm.sn" auto-complete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="saveFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
            </div>
        </el-dialog>

        <!--新增/修改界面2-->
        <el-dialog :title="title" :visible.sync="saveFormDVisible" :close-on-click-modal="false">
            <el-form :model="saveFormD" label-width="80px" :rules="saveFormDRules" ref="saveFormD">
                <el-form-item label="编号" prop="name">
                    <el-input v-model="saveFormD.typesId" auto-complete="off">
                        <el-select  v-model="saveForm.employee" value-key="id" clearable placeholder="请选择">
                            <el-cascader
                                    v-model="saveForm.department"
                                    :options="deptTree"
                                    :props="{checkStrictly: true,value:'id',label:'name'}"
                                    clearable></el-cascader>
                        </el-select>
                    </el-input>
                </el-form-item>
                <el-form-item label="字典编号" prop="name">
                    <el-input v-model="saveFormD.name" auto-complete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="saveFormDVisible = false">取消</el-button>
                <el-button type="primary" @click.native="saveSubmits" :loading="saveLoading">提交</el-button>
            </div>
        </el-dialog>

        <el-drawer
                :visible.sync="tables"
                direction="rtl"
                size="50%">
            <!--工具条-->
            <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
                <el-form :inline="true" :model="filtersd">
                    <el-form-item>
                        <el-input v-model="filtersd.keyWords" placeholder="数据字典名"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" v-on:click="searchd">查询</el-button>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="handleAdds">新增</el-button>
                    </el-form-item>
                </el-form>
            </el-col>

            <el-table :data="gridData">
                <el-table-column property="date" label="编号" width="150"></el-table-column>
                <el-table-column property="name" label="明细名" width="300"></el-table-column>
                <el-table-column label="操作">
                    <template scope="scope">
                        <el-button size="small" @click="handleEditD(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="danger" size="small" @click="handleDelD(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <!--工具条-->
            <el-col :span="24" class="toolbar">
                <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
                <el-pagination layout="prev, pager, next"
                               @current-change="handleCurrentChange"
                               :page-size="pageSize" :total="total"
                               :current-page="currentPage"
                               style="float:right;">
                </el-pagination>
            </el-col>
        </el-drawer>
    </section>
</template>

<script>
    import util from '../../common/js/util'
    //import NProgress from 'nprogress'
    import { getDepartmentListPage, removeDepartment, batchRemoveDepartment, editDepartment, addDepartment } from '../../api/api';

    export default {
        data() {
            return {
                deptTree: [],
                tables: false,
                gridData: [

                ],
                filters: {
                    keyWords: ''
                },
                filtersd: {
                    keyWords: ''
                },
              /*  deptTree: [],*/
                departments: [],
                total: 0,
                currentPage: 1,
                pageSize:10,
                listLoading: false,
                sels: [],//列表选中列

                saveFormVisible: false,//新增界面是否显示
                saveFormDVisible: false,//新增界面是否显示
                saveLoading: false,
                saveFormRules: {
                    name: [
                        { required: true, message: '请输入数据字典名', trigger: 'blur' }
                    ],
                    sn: [
                        { required: true, message: '请输入编号', trigger: 'blur' }
                    ]
                },

                saveFormDRules: {
                    name: [
                        { required: true, message: '请输入数据字典名', trigger: 'blur' }
                    ],
                    typesId: [
                        { required: true, message: '请输入字典编号', trigger: 'blur' }
                    ]
                },
                //新增界面数据
                saveForm: {
                    title:"",
                    id: null,
                    name: '',
                   /* sn: "",
                    department: null*/
                },

                saveFormD: {
                    title:"",
                    id: null,
                    name: '',
                    typesId: null,
                }

            }
        },
        methods: {
            getDeptTree(){
                this.$http.get("/type/tree")
                    .then(result=>{
                        this.deptTree = result.data;
                    })
            },

            getemployee(){
                this.$http.get("/employee/index")
                    .then(result=>{
                        this.employees=result.data;
                    })

            },
            search(){
                this.currentPage = 1;
                this.getDepartments();
            },

            searchd(){
                this.currentPage = 1;
                this.getDictionaryDetail();
            },
            handleCurrentChange(val) {
                this.currentPage = val;
                this.getDepartments();
            },
            //获取部门列表
            getDepartments() {
                let para = {
                    currentPage: this.currentPage,
                    pageSize: this.pageSize,
                    keyWords: this.filters.keyWords
                };

                this.listLoading = true;//开启忙等框
                //NProgress.start();
                //发送axios请求从后端  8080 拿数据
                this.$http.post("/type/page",para)
                    .then(result=>{
                        this.listLoading = false;//关闭忙等框
                        result = result.data;//PageList
                        this.total =result.total;
                        this.departments = result.rows;

                    })
                    .catch(result=>{
                        console.log(result);
                        this.$message({
                            type: 'error',
                            message: '系统错误,正在殴打程序员!!'
                        });
                    });


                /*getDepartmentListPage(para).then((res) => {
                    this.total = res.data.total;
                    this.departments = res.data.departments;
                    this.listLoading = false;
                    //NProgress.done();
                });*/
            },

            //获取明细列表2
            getDictionaryDetail() {
                let para = {
                    currentPage: this.currentPage,
                    pageSize: this.pageSize,
                    keyWords: this.filtersd.keyWords
                };

                this.listLoading = true;//开启忙等框
                //NProgress.start();
                //发送axios请求从后端  8080 拿数据
                this.$http.post("/detail/page",para)
                    .then(result=>{
                        this.listLoading = false;//关闭忙等框
                        result = result.data;//PageList
                        this.total =result.total;
                        this.gridData = result.rows;

                    })
                    .catch(result=>{
                        console.log(result);
                        this.$message({
                            type: 'error',
                            message: '系统错误,正在殴打程序员!!'
                        });
                    });


                /*getDepartmentListPage(para).then((res) => {
                    this.total = res.data.total;
                    this.departments = res.data.departments;
                    this.listLoading = false;
                    //NProgress.done();
                });*/
            },

            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    this.$http.delete("/type/"+row.id)
                        .then(result=>{
                            this.listLoading = false;
                            if(result.data.message){
                                this.getDepartments();
                                this.$message({
                                    message:result.data.message,
                                    type:'success'
                                })
                            }else {
                                //删除失败提示
                                this.$message({
                                    message:result.data.message,
                                    type: 'error'
                                })
                            }
                        }).catch(result=>{
                        this.$message({
                            message: '系统错误,正在殴打程序员',
                            type: 'error'
                        })
                    })
                }).catch(() => {

                });
            },

            //删除2
            handleDelD: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    this.$http.delete("/detail/"+row.id)
                        .then(result=>{
                            this.listLoading = false;
                            if(result.data.message){
                                this.getDictionaryDetail();
                                this.$message({
                                    message:result.data.message,
                                    type:'success'
                                })
                            }else {
                                //删除失败提示
                                this.$message({
                                    message:result.data.message,
                                    type: 'error'
                                })
                            }
                        }).catch(result=>{
                        this.$message({
                            message: '系统错误,正在殴打程序员',
                            type: 'error'
                        })
                    })
                }).catch(() => {

                });
            },


            //显示编辑界面
            handleEdit: function (index, row) {
                this.title = "编辑";
                this.saveFormVisible = true;
                this.saveForm = Object.assign({}, row);
                /*if(row.dirPath){
                    let array = row.dirPath.split("/");
                    let tmp = [];
                    for(let i=1;i<array.length-1;i++){
                        tmp.push(parseInt(array[i]));
                    }
                    console.log(tmp)
                    this.saveForm.department = tmp;

                }*/
            },

            //显示编辑界面2
            handleEditD: function (index, row) {
                this.getDeptTree();
                this.title = "编辑";
                this.saveFormDVisible = true;
                this.saveFormD = Object.assign({}, row);
                if(row.dictionaryType){
                    this.saveForm.department = tmp;

                }
            },

            //显示新增界面
            handleAdd: function () {
                this.title = "新增";
                this.saveFormVisible = true;
                this.saveForm = {
                    name: '',
                    sn: "",
                };
            },

            handleAdds: function () {
                this.getDeptTree();
                this.title = "新增";
                this.saveFormDVisible = true;
                this.saveFormD = {
                    name: '',
                    typesId: null,
                };
            },
            //新增
            saveSubmit: function () {
                this.$refs.saveForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.saveLoading = true;
                            //NProgress.start();
                            let para = Object.assign({}, this.saveForm);
                            /*if(para.department){
                                para.department={"id":para.department[para.department.length-1]}
                            }*/
                            this.$http.post("/type/save",para)
                                .then(result=>{

                                    this.listLoading = false;
                                    this.saveLoading = false;
                                    if(result.data.success){
                                        this.getDepartments();
                                        this.$message({
                                            message: result.data.message,
                                            type: 'success'
                                        });
                                        this.saveFormVisible=false;
                                    }else {
                                        this.listLoading = false;
                                        this.$message({
                                            message: result.data.message,
                                            type: 'error'
                                        });
                                    }
                                }).catch(result=>{
                                this.listLoading = false;
                                this.saveLoading = false;
                                this.$message({
                                    message: '系统错误,正在殴打程序员',
                                    type: 'error'
                                });
                            })
                        });
                    }
                });
            },

            saveSubmits: function () {
                this.$refs.saveFormD.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.saveLoading = true;
                            //NProgress.start();
                            let para = Object.assign({}, this.saveFormD);
                            console.log(para);
                            /*if(para.department){
                                para.department={"id":para.department[para.department.length-1]}
                            }*/
                            this.$http.post("/detail/save",para)
                                .then(result=>{

                                    this.listLoading = false;
                                    this.saveLoading = false;
                                    if(result.data.success){
                                        this.getDictionaryDetail();
                                        this.$message({
                                            message: result.data.message,
                                            type: 'success'
                                        });
                                        this.saveFormDVisible=false;
                                    }else {
                                        this.listLoading = false;
                                        this.$message({
                                            message: result.data.message,
                                            type: 'error'
                                        });
                                    }
                                }).catch(result=>{
                                this.listLoading = false;
                                this.saveLoading = false;
                                this.$message({
                                    message: '系统错误,正在殴打程序员',
                                    type: 'error'
                                });
                            })
                        });
                    }
                });
            },

            selsChange: function (sels) {
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id);
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    //let para = { ids: ids };
                    this.$http.patch("/type",ids)
                        .then(result=>{
                            this.listLoading = false;
                            if(result.data.success){
                                this.getDepartments();
                                this.$message({
                                    message: result.data.message,
                                    type: 'success'
                                });
                            }else {
                                this.listLoading = false;
                                this.$message({
                                    message: result.data.message,
                                    type: 'error'
                                });
                            }
                        }).catch(result=>{
                        this.listLoading = false;
                        this.$message({
                            message: '系统错误,正在殴打程序员',
                            type: 'error'
                        });
                    })
                    /*batchRemoveDepartment(para).then((res) => {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getDepartments();
                    });*/
                }).catch(() => {

                });
            }
        },
        mounted() {
            this.getDepartments();
            this.getDictionaryDetail();
            this.getemployee();
            this.getDeptTree();
        }
    }

</script>

<style scoped>

</style>
